package gui;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import data.User;

/**
 * COPYRIGHT (C) 2009. Denny Tran, Omar Jaime, David Deng, Julie Hsieh. All Rights Reserved
 * Class is a ViewPanel containing JComponents asking for password information.  
 * @author Denny Tran, Omar Jaime, David Deng, Julie Hsieh 
 * @version 1.01 2009-05-05
 */

public class ChangePassword extends ViewPanel
{

    private JPasswordField oldPwdField;
    private JPasswordField newPwdField;
    private JPasswordField confirmPwdField;

    /**
     * Construct ViewPanel containing JComponents asking for password information
     * @param viewManager the container of this panel
     * @param identification specific identification used to reference this panel
     */
    public ChangePassword(final ViewManager viewManager, String identification)
    {
        super(viewManager, identification);

        Dimension labelSize = new Dimension(140, 20);
        int labelX = 60;
        int labelY = 80;
        int labelSpread = 30;
        
        // labels
        JLabel title = new JLabel("Change Password");
        title.setFont(new Font("Serif", Font.PLAIN, 24));
        title.setSize(250, 30);
        title.setLocation(60, 0);
        this.add(title);
        
        JLabel oldPwdLabel = new JLabel("Old Password:");
        oldPwdLabel.setSize(labelSize);
        oldPwdLabel.setLocation(labelX,labelY);
        labelY += labelSpread;
        this.add(oldPwdLabel);
    
        JLabel newPwdLabel = new JLabel("New Password:");
        newPwdLabel.setSize(labelSize);
        newPwdLabel.setLocation(labelX, labelY);
        labelY += labelSpread;
        this.add(newPwdLabel);
        
        JLabel confirmPwdLabel = new JLabel("Confirm New Password:");
        confirmPwdLabel.setSize(labelSize);
        confirmPwdLabel.setLocation(labelX, labelY);
        labelY += labelSpread;
        this.add(confirmPwdLabel);
        
        
        // text fields
        Dimension textFieldSize = new Dimension(150,20);
        int textFieldX = labelX + (int)labelSize.getWidth() + 20;
        int textFieldY = 80;
        // use same spread as labels
        
        oldPwdField = new JPasswordField();
        oldPwdField.setSize(textFieldSize);
        oldPwdField.setLocation(textFieldX, textFieldY);
        textFieldY += labelSpread;
        this.add(oldPwdField);
        
        newPwdField = new JPasswordField();
        newPwdField.setSize(textFieldSize);
        newPwdField.setLocation(textFieldX, textFieldY);
        textFieldY += labelSpread;
        this.add(newPwdField);
        
        confirmPwdField = new JPasswordField();
        confirmPwdField.setSize(textFieldSize);
        confirmPwdField.setLocation(textFieldX, textFieldY);
        textFieldY += labelSpread;
        this.add(confirmPwdField);
        
        // buttons
        
        JButton changePwdButton = new JButton("OK");
        changePwdButton.setSize(80,20);
        changePwdButton.setLocation(100, 300);
        changePwdButton.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent arg0)
            {
                String oldPwd = oldPwdField.getText();
                String newPwd = newPwdField.getText();
                String confirm = confirmPwdField.getText();
                
                if (oldPwd.isEmpty())
                {
                    JOptionPane.showMessageDialog(null, "Old Password field is empty.");
                    return;
                }
                if (newPwd.isEmpty())
                {
                    JOptionPane.showMessageDialog(null, "New Password field is empty.");
                    return;
                }
                if (confirm.isEmpty())
                {
                    JOptionPane.showMessageDialog(null, "Confirm Password field is empty.");
                    return;
                }
                
                
                if (!newPwd.equals(confirm))
                {
                    JOptionPane.showMessageDialog(null, "New Password and confirm password are not the same.");
                    return;
                }
                else
                {
                   String username = viewManager.getGs().getUser().getUserName();
                   User user = null;
                   try
                  {
                     user = viewManager.getGs().getMySql().getUser(username, oldPwd);
                  } catch (SQLException e)
                  {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                  }
                   
                   if(user !=  null)
                   {
                      try
                     {
                        viewManager.getGs().getMySql().updateUser(user, newPwd);
                        JOptionPane.showMessageDialog(null, "Password updated!");
                     } catch (SQLException e)
                     {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                     }
                   }
                   else
                      JOptionPane.showMessageDialog(null, "Incorrect password!");
                }
                
                
            }
        
        });
        this.add(changePwdButton);
        
        JButton cancelButton = new JButton("Cancel");
        cancelButton.setSize(80,20);
        cancelButton.setLocation(200, 300);
        cancelButton.addActionListener(new ActionListener()
        {

           @Override
           public void actionPerformed(ActionEvent e)
           {
              User u = viewManager.getGs().getUser();
              EditProfile view = (EditProfile)viewManager.getView("edit profile");
              viewManager.showView("edit profile");
              view.setView(u);
           }
       });
        this.add(cancelButton);
    }

    /**
     * Clears all text fields and check boxes
     */
    public void clearView()
    {
        oldPwdField.setText("");
        newPwdField.setText("");
        confirmPwdField.setText("");
    }

}